package com.younion.business.impl;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.util.CollectionUtils;

import com.younion.business.IUserService;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.util.MD5;
import com.younion.vo.UserVO;

import cpcns.jhsks.util.MapCollectionUtils;

 
public class UserServiceImpl extends BaseService implements IUserService {

	protected static final Log logger = LogFactory
			.getLog(UserServiceImpl.class);

	public Collection getAll() throws Exception {
		return getUserDAO().selectAll();
	}

	public UserVO getById(String id) throws BaseException {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id) < 1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = getUserDAO().selectById(paramMap);
			return obj == null ? null : (UserVO) obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(UserServiceImpl.class.getName()
					+ " >> public UserVO get(String id) error occur:", e);
		}
	}

	public UserVO getByUser(UserVO user) throws BaseException {
		try {
			Object obj = getUserDAO().selectByUser(user);
			return obj == null ? null : (UserVO) obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(UserServiceImpl.class.getName()
					+ " >> public UserVO get(String id) error occur:", e);
		}
	}

	public Collection getByMap(Map paramMap) throws BaseException {
		if (CollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			return getUserDAO().selectByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(UserServiceImpl.class.getName()
					+ " >> public Collection getByMap(Map map) error occur:", e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return getUserDAO().selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					UserServiceImpl.class.getName()
							+ " >> public Collection getCountByMap(Map map) error occur:",
					e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		if (MapCollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return getUserDAO().selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					UserServiceImpl.class.getName()
							+ " >> public Page get(Map paramMap, Integer curPage, Integer pageSize) error occur:",
					e);
		}
	}

	public UserVO save(UserVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			if(voObject.getPassword() == null || "".equals(voObject.getPassword())){
				voObject.setPassword("123456");
			}
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(voObject));
			if (id != null) {
				if(IUserService.PASSWORD_STR.equals(voObject.getPassword())){
					paramMap.remove("password");
				}else{
					paramMap.put("password", new Md5Hash(voObject
							.getPassword()).toHex());
				}
				getUserDAO().updateByMap(paramMap);
				return voObject;
			} else {
				voObject.setUserToken(new Md5Hash(voObject.getAccount() + voObject
						.getPassword()).toHex());
				voObject.setPassword(new Md5Hash(voObject
						.getPassword()).toHex());
				if(voObject.getCustomer() == null)
					voObject.setCustomer(getCurrectUser().getCustomer());
				voObject.setStatus(1);
				voObject.setCreateDate(new Date());
				return (UserVO) getUserDAO().insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(UserServiceImpl.class.getName()
					+ " >> public UserVO save(UserVO voObject) error occur:", e);
		}
	}

	public Collection save(Collection voList) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				UserVO data = (UserVO) it.next();
				data = save(data);
			}
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					UserServiceImpl.class.getName()
							+ " >> public Collection save(Collection voList) error occur:",
					e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try {
			getUserDAO().deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(UserServiceImpl.class.getName()
					+ " >> public boolean remove(List ids) error occur:", e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			getUserDAO().deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					UserServiceImpl.class.getName()
							+ " >> public boolean removeByMap(Map paramMap) error occur:",
					e);
		}
	}

	public void insertVos(List<UserVO> users) throws Exception {
		try {
			getUserDAO().batchInsert(users);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					UserServiceImpl.class.getName()
							+ " >> public Collection save(Collection voList) error occur:",
					e);
		}
	}

}